package com.scau.yangsuiyu.estore.service;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.controller.*;
import com.scau.yangsuiyu.estore.controller.buyer.BuyGoodsPane;
import com.scau.yangsuiyu.estore.controller.buyer.BuyerLayoutController;
import com.scau.yangsuiyu.estore.controller.buyer.CartPane;
import com.scau.yangsuiyu.estore.controller.buyer.CommentGoodsPane;
import com.scau.yangsuiyu.estore.controller.seller.SellerLayoutController;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Goods;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.dababases.entity.User;
import javafx.scene.control.Tab;

/**
 * @author suiyu_yang
 * @description 提供前端访问后台的服务
 * @date 2019-06-03 18:32
 * @email suiyu_yang@163.com
 */
public class AppService {
    private static MainStageController mainStageController;

    public static void start(MainStageController _mainStageController) {
        mainStageController = _mainStageController;
    }

    public void gotoLogin() {
        Tab loginTab = new Tab();
        loginTab.setText("登录界面 - 淘，你所爱！");
        loginTab.setContent(new LoginController());

        mainStageController.gotoNewPage(loginTab);
    }


    public void gotoRegister() {
        Tab registerTab = new Tab();
        registerTab.setText("注册界面 - 淘，你所爱！");
        registerTab.setContent(new RegisterController());

        mainStageController.gotoNewPage(registerTab);
    }

    public void gotoIndexPage() {
        Tab indexTab = new Tab();
        indexTab.setText("myEStore在线购物系统 - 首页");
        indexTab.setContent(new IndexViewController());

        mainStageController.gotoNewPage(indexTab);
    }

    public void gotoIndexPage(User user) {
        Tab indexTab = new Tab();
        indexTab.setText("myEStore在线购物系统 - 首页");
        indexTab.setContent(new IndexViewController(user));

        mainStageController.gotoNewPage(indexTab);
    }

    public void gotoBuyGoodsPane(User user, Goods goods) {
        Tab buyGoodsTab = new Tab();
        buyGoodsTab.setText("商品界面-淘，你所爱");
        buyGoodsTab.setContent(new BuyGoodsPane(user, goods));

        mainStageController.gotoNewPage(buyGoodsTab);
    }

    public void gotoCommentGoodsPane() {
        Tab commentGoodsTab = new Tab();
        commentGoodsTab.setText("评价商品界面-淘，你所爱");
        commentGoodsTab.setContent((new CommentGoodsPane()));

        mainStageController.gotoNewPage(commentGoodsTab);
    }

    public void gotoCartPane(Buyer buyer) {
        Tab cartTab = new Tab();
        cartTab.setText("购物车界面-淘，你所爱");
        cartTab.setContent(new CartPane(buyer));

        mainStageController.gotoNewPage(cartTab);
    }


    public void gotoSellerLayout(JSONObject jsonSellerObject) {
        Tab sellerTab = new Tab();
        sellerTab.setText("卖家界面");

        Seller seller = (Seller) jsonSellerObject.getObject("user", Seller.class);
        if (seller != null)
            sellerTab.setText(seller.getUsername() + " - 卖家界面");

        sellerTab.setContent(new SellerLayoutController(jsonSellerObject));

        mainStageController.gotoNewPage(sellerTab);
    }

    public void gotoBuyerLayout(JSONObject jsonBuyerObject) {
        Tab buyerTab = new Tab();
        buyerTab.setText("买家界面");

        Buyer buyer = jsonBuyerObject.getObject("user", Buyer.class);
        if (buyer != null)
            buyerTab.setText(buyer.getUsername() + " - 买家界面");

        buyerTab.setContent(new BuyerLayoutController(jsonBuyerObject));

        mainStageController.gotoNewPage(buyerTab);
    }

    public void closeCurrentTab() {
        mainStageController.removeCurrentPage();
    }
}
